package com.lwj.springbootvue.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.lwj.springbootvue.constants.HttpStatus;
import com.lwj.springbootvue.exception.ServiceException;
import com.lwj.springbootvue.mapper.StudentMapper;
import com.lwj.springbootvue.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lwj.springbootvue.mapper.ClasseMapper;
import com.lwj.springbootvue.service.IClasseService;
import com.lwj.springbootvue.model.Classe;
import org.springframework.transaction.annotation.Transactional;

/**
 * 班级Service业务层处理
 * 
 * @author king
 * @date 2024-12-25
 */
@Service
public class ClasseServiceImpl implements IClasseService 
{
    @Autowired
    private ClasseMapper classeMapper;

    @Autowired
    private StudentMapper studentMapper;



    /**
     * 查询班级总人数
     *
     * @param
     * @return 班级
     */
    @Transactional
    public int updateClasseNumByScid(){
        // 1-6 个班级
        for(int i=1;i<=6;i++){
            // 查询属于 cid 班级的总人数
            Student student = new Student();
            student.setScid((long) i);
            List<Student> students = studentMapper.selectStudentList(student);
            // 2.更新num
            if(ObjectUtil.isNull(student)){
                throw new ServiceException(HttpStatus.ERROR,"班级人数为学生0");
            }
            // 3.更新
            Classe classe = new Classe();
            classe.setNum(students.size()); // 设置班级总人数
            classe.setCid((long)i);
            int i1 = classeMapper.updateClasse(classe);
            if(i1==0){
                throw new ServiceException(HttpStatus.ERROR,"班级人数更新失败！");
            }
        }
        return 1;
    }



    /**
     * 查询班级
     * 
     * @param cid 班级主键
     * @return 班级
     */
    @Override
    public Classe selectClasseByCid(Long cid)
    {
        return classeMapper.selectClasseByCid(cid);
    }

    /**
     * 查询班级列表
     * 
     * @param classe 班级
     * @return 班级
     */
    @Override
    public List<Classe> selectClasseList(Classe classe)
    {
        return classeMapper.selectClasseList(classe);
    }

    /**
     * 新增班级
     * 
     * @param classe 班级
     * @return 结果
     */
    @Override
    public int insertClasse(Classe classe)
    {
        classe.setCreateTime(LocalDateTimeUtil.now());
        return classeMapper.insertClasse(classe);
    }

    /**
     * 修改班级
     * 
     * @param classe 班级
     * @return 结果
     */
    @Override
    public int updateClasse(Classe classe)
    {
        return classeMapper.updateClasse(classe);
    }

    /**
     * 批量删除班级
     * 
     * @param cids 需要删除的班级主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteClasseByCids(Long[] cids)
    {
        // 删除班级时，删除对应的学生
        for(Long cid:cids){
            // 删除班级时，删除对应的学生
            Student student = new Student();
            student.setScid(cid);
            // 1.查询所在删除班级的学生
            List<Student> students = studentMapper.selectStudentList(student);
            // 2.全部删除
            List<Long> longList = new ArrayList<Long>(); //记录删除学生列表的 学生id
            students.forEach(stu -> longList.add(stu.getSid()));
            Long[] count = longList.toArray(new Long[0]);
            // 3.删除全部学生
            int i = studentMapper.deleteStudentBySids(count);
            System.out.println("删除班级成功！对应的删除的学生人数为： "+i);
        }
        return classeMapper.deleteClasseByCids(cids);
    }

    /**
     * 删除班级信息
     * 
     * @param cid 班级主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteClasseByCid(Long cid)
    {
        // 删除班级时，删除对应的学生
        Student student = new Student();
        student.setScid(cid);
            // 1.查询所在删除班级的学生
        List<Student> students = studentMapper.selectStudentList(student);
            // 2.全部删除
        List<Long> longList = new ArrayList<Long>(); //记录删除学生列表的 学生id
        students.forEach(stu -> longList.add(stu.getSid()));
        Long[] count = longList.toArray(new Long[0]);
            // 3.删除全部学生
        int i = studentMapper.deleteStudentBySids(count);
        System.out.println("删除班级成功！对应的删除的学生人数为： "+i);
        return classeMapper.deleteClasseByCid(cid);
    }


}
